<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'

require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

import { GetIMLoginByMonth } from '@/api/Report'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      chart: null,
      dateTime: '一个月'
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')

      this.setOptions()
    },
    setOptions() {
      new GetIMLoginByMonth().then(res => {
        if (res.statusCode === 200) {
          this.chart.setOption({
            title: {
              text: `近 ${this.dateTime} 登录统计`,
              subtextStyle: {
                color: '#aaa'
              }
            },
            dataset: {
              // dimensions: ['LoginTime', 'Count'],
              source: res.data
            },
            legend: {},
            tooltip: {
              trigger: 'axis',
              axisPointer: {
                type: 'cross'
              }
            },
            xAxis: {
              type: 'category',
              boundaryGap: false,
              axisLabel: {}
            },
            yAxis: {
              type: 'value',
              boundaryGap: [0, '100%']
            },
            grid: {
              left: 10,
              right: 10,
              bottom: 20,
              containLabel: true
            },
            series: [
              {
                name: '登入数',
                type: 'line',
                encode: {
                  x: 'LoginTime',
                  y: 'Count'
                }
              }
            ]
          })
        }
      })
    }
  }
}
</script>
